home *** CD-ROM | disk | FTP | other *** search
/ X User Tools / X User Tools (O'Reilly and Associates)(1994).ISO / sun4c / archive / tcltk.z / tcltk / man / catn / expr.n < prev    next >
Encoding:
Text File  |  1994-09-20  |  13.4 KB  |  397 lines

  1.  
  2.  
  3.  
  4. expr(n)               Tcl Built-In Commands                   7.0
  5.  
  6.  
  7.  
  8. _________________________________________________________________
  9.  
  10. NAME
  11.      expr - Evalue an expression
  12.  
  13. SYNOPSIS
  14.      expr _a_r_g ?_a_r_g _a_r_g ...?
  15. _________________________________________________________________
  16.  
  17.  
  18. DESCRIPTION
  19.      Concatenates _a_r_g's (adding separator spaces  between  them),  |
  20.      evaluates  the  result  as a Tcl expression, and returns the  |
  21.      value.  The operators permitted in  Tcl  expressions  are  a
  22.      subset of the operators permitted in C expressions, and they
  23.      have the same meaning and precedence as the corresponding  C
  24.      operators.   Expressions almost always yield numeric results
  25.      (integer  or  floating-point  values).   For  example,   the
  26.      expression
  27.  
  28.           expr 8.2 + 6
  29.  
  30.      evaluates to 14.2.  Tcl expressions differ  from  C  expres-
  31.      sions  in  the  way  that operands are specified.  Also, Tcl
  32.      expressions support non-numeric  operands  and  string  com-
  33.      parisons.
  34.  
  35. OPERANDS
  36.      A Tcl expression consists  of  a  combination  of  operands,
  37.      operators, and parentheses.  White space may be used between
  38.      the operands and operators and parentheses; it is ignored by
  39.      the  expression  processor.   Where  possible,  operands are
  40.      interpreted as integer values.  Integer values may be speci-
  41.      fied  in  decimal  (the normal case), in octal (if the first
  42.      character of the operand is 0), or in  hexadecimal  (if  the
  43.      first  two characters of the operand are 0x).  If an operand
  44.      does not have one of the integer formats given  above,  then
  45.      it  is  treated as a floating-point number if that is possi-
  46.      ble.  Floating-point numbers may be specified in any of  the
  47.      ways  accepted  by an ANSI-compliant C compiler (except that
  48.      the ``f'', ``F'', ``l'', and ``L'' suffixes will not be per-
  49.      mitted in most installations).  For example, all of the fol-
  50.      lowing are valid  floating-point  numbers:   2.1,  3.,  6e4,
  51.      7.91e+16.  If no numeric interpretation is possible, then an
  52.      operand is left as a string  (and  only  a  limited  set  of
  53.      operators may be applied to it).
  54.  
  55.      Operands may be specified in any of the following ways:
  56.  
  57.      [1]  As an numeric value, either integer or floating-point.
  58.  
  59.      [2]  As a Tcl variable,  using  standard  $  notation.   The
  60.           variable's value will be used as the operand.
  61.  
  62.  
  63.  
  64. Tcl                                                             1
  65.  
  66.  
  67.  
  68.  
  69.  
  70. expr(n)               Tcl Built-In Commands                   7.0
  71.  
  72.  
  73.  
  74.      [3]  As a string enclosed in double-quotes.  The  expression
  75.           parser  will  perform  backslash, variable, and command
  76.           substitutions on the information  between  the  quotes,
  77.           and use the resulting value as the operand
  78.  
  79.      [4]  As a string enclosed in braces.  The characters between
  80.           the open brace and matching close brace will be used as
  81.           the operand without any substitutions.
  82.  
  83.      [5]  As a Tcl command enclosed  in  brackets.   The  command
  84.           will  be  executed  and  its result will be used as the
  85.           operand.
  86.  
  87.      [6]  As a mathematical function whose arguments have any  of  |
  88.           the above forms for operands, such as ``sin($x)''.  See  |
  89.           below for a list of defined functions.
  90.  
  91.      Where  substitutions  occur  above   (e.g.   inside   quoted
  92.      strings),  they  are  performed by the expression processor.
  93.      However, an additional layer  of  substitution  may  already
  94.      have been performed by the command parser before the expres-
  95.      sion processor was called.  As discussed below, it  is  usu-
  96.      ally  best  to  enclose expressions in braces to prevent the
  97.      command parser from performing  substitutions  on  the  con-
  98.      tents.
  99.  
  100.      For some examples of simple expressions, suppose  the  vari-
  101.      able  a  has the value 3 and the variable b has the value 6.
  102.      Then the command on the left side of each of the lines below
  103.      will produce the value on the right side of the line:
  104.  
  105.           expr 3.1 + $a           6.1
  106.           expr 2 + "$a.$b"        5.6
  107.           expr 4*[llength "6 2"]  8
  108.           expr {{word one} < "word $a"}0
  109.  
  110.  
  111. OPERATORS
  112.      The valid operators are listed below, grouped in  decreasing
  113.      order of precedence:
  114.  
  115.      -  ~  !             Unary minus, bit-wise NOT, logical  NOT.
  116.                          None of these operands may be applied to
  117.                          string operands, and bit-wise NOT may be
  118.                          applied only to integers.
  119.  
  120.      *  /  %             Multiply, divide,  remainder.   None  of
  121.                          these  operands may be applied to string
  122.                          operands, and remainder may  be  applied
  123.                          only  to  integers.   The remainder will  |
  124.                          always have the same sign as the divisor  |
  125.                          and  an  absolute value smaller than the  |
  126.                          divisor.
  127.  
  128.  
  129.  
  130. Tcl                                                             2
  131.  
  132.  
  133.  
  134.  
  135.  
  136. expr(n)               Tcl Built-In Commands                   7.0
  137.  
  138.  
  139.  
  140.      +  -                Add and subtract.  Valid for any numeric
  141.                          operands.
  142.  
  143.      <<  >>              Left and right shift.  Valid for integer
  144.                          operands only.
  145.  
  146.      <  >  <=  >=        Boolean  less,  greater,  less  than  or
  147.                          equal,  and greater than or equal.  Each
  148.                          operator produces 1 if the condition  is
  149.                          true,  0 otherwise.  These operators may
  150.                          be applied to strings as well as numeric
  151.                          operands,  in  which  case  string  com-
  152.                          parison is used.
  153.  
  154.      ==  !=              Boolean  equal  and  not  equal.    Each
  155.                          operator  produces  a  zero/one  result.
  156.                          Valid for all operand types.
  157.  
  158.      &                   Bit-wise   AND.    Valid   for   integer
  159.                          operands only.
  160.  
  161.      ^                   Bit-wise  exclusive   OR.    Valid   for
  162.                          integer operands only.
  163.  
  164.      |                   Bit-wise OR.  Valid for integer operands
  165.                          only.
  166.  
  167.      &&                  Logical AND.  Produces  a  1  result  if
  168.                          both operands are non-zero, 0 otherwise.
  169.                          Valid   for   numeric   operands    only
  170.                          (integers or floating-point).
  171.  
  172.      ||                  Logical OR.  Produces a 0 result if both
  173.                          operands  are  zero, 1 otherwise.  Valid
  174.                          for numeric operands only  (integers  or
  175.                          floating-point).
  176.  
  177.      _x?_y:_z               If-then-else, as in C.  If  _x  evaluates
  178.                          to  non-zero,  then  the  result  is the
  179.                          value of _y.  Otherwise the result is the
  180.                          value  of  _z.  The _x operand must have a
  181.                          numeric value.
  182.  
  183.      See the C manual for more details on the results produced by
  184.      each  operator.   All of the binary operators group left-to-
  185.      right within the same precedence level.   For  example,  the
  186.      command
  187.  
  188.           expr 4*2 < 7
  189.  
  190.      returns 0.
  191.  
  192.  
  193.  
  194.  
  195. Tcl                                                             3
  196.  
  197.  
  198.  
  199.  
  200.  
  201.  
  202. expr(n)               Tcl Built-In Commands                   7.0
  203.  
  204.  
  205.  
  206.      The &&, ||, and ?: operators have ``lazy evaluation'',  just
  207.      as in C, which means that operands are not evaluated if they
  208.      are not needed to determine the outcome.   For  example,  in
  209.      the command
  210.  
  211.           expr {$v ? [a] : [b]}
  212.  
  213.      only one of [a] or [b] will actually be evaluated, depending
  214.      on  the  value of $v.  Note, however, that this is only true
  215.      if the entire expression is enclosed in  braces;   otherwise
  216.      the  Tcl parser will evaluate both [a] and [b] before invok-
  217.      ing the expr command.
  218.  
  219. MATH FUNCTIONS
  220.      Tcl supports the following mathematical functions in expres-  |
  221.      sions:                                                        |
  222.  
  223.           acos        cos         hypot      sinh                  |
  224.           asin        cosh        log        sqrt                  |
  225.           atan        exp         log10      tan                   |
  226.           atan2       floor       pow        tanh                  |
  227.           ceil        fmod        sin                              |
  228.      Each of these functions invokes the math library function of  |
  229.      the same name;  see the manual entries for the library func-  |
  230.      tions for details on what they do.  Tcl also implements  the  |
  231.      following  functions  for  conversion  between  integers and  |
  232.      floating-point numbers:                                       |
  233.  
  234.      abs(_a_r_g)                                                           ||
  235.           Returns  the  absolute value of _a_r_g.  _A_r_g may be either  |
  236.           integer or floating-point, and the result  is  returned  |
  237.           in the same form.                                        |
  238.  
  239.      double(_a_r_g)                                                        ||
  240.           If _a_r_g is a floating value, returns _a_r_g, otherwise con-  |
  241.           verts _a_r_g to floating and returns the converted value.   |
  242.  
  243.      int(_a_r_g)                                                           ||
  244.           If _a_r_g is an integer value, returns _a_r_g, otherwise con-  |
  245.           verts _a_r_g to integer by truncation and returns the con-  |
  246.           verted value.                                            |
  247.  
  248.      round(_a_r_g)                                                         ||
  249.           If _a_r_g is an integer value, returns _a_r_g, otherwise con-  |
  250.           verts _a_r_g to integer by rounding and returns  the  con-  |
  251.           verted value.                                            |
  252.  
  253.      In addition to these predifined functions, applications  may  |
  254.      define additional functions using Tcl_CreateMathFunc().
  255.  
  256. TYPES, OVERFLOW, AND PRECISION
  257.  
  258.  
  259.  
  260.  
  261. Tcl                                                             4
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268. expr(n)               Tcl Built-In Commands                   7.0
  269.  
  270.  
  271.  
  272.      All internal computations involving integers are  done  with
  273.      the  C  type  _l_o_n_g,  and all internal computations involving
  274.      floating-point are done with the C type _d_o_u_b_l_e.   When  con-
  275.      verting  a  string  to  floating-point, exponent overflow is
  276.      detected and results in a  Tcl  error.   For  conversion  to
  277.      integer  from  string,  detection of overflow depends on the
  278.      behavior of some routines in the  local  C  library,  so  it
  279.      should  be  regarded  as  unreliable.   In any case, integer
  280.      overflow and underflow are generally not  detected  reliably
  281.      for   intermediate  results.   Floating-point  overflow  and
  282.      underflow are  detected  to  the  degree  supported  by  the
  283.      hardware, which is generally pretty reliable.
  284.  
  285.      Conversion  among  internal  representations  for   integer,
  286.      floating-point, and string operands is done automatically as
  287.      needed.  For  arithmetic  computations,  integers  are  used
  288.      until  some floating-point number is introduced, after which
  289.      floating-point is used.  For example,
  290.  
  291.           expr 5 / 4
  292.  
  293.      returns 1, while
  294.  
  295.           expr 5 / 4.0
  296.           expr 5 / ( [string length "abcd"] + 0.0 )
  297.      both return 1.25.  Floating-point values are always returned  |
  298.      with  a  ``.''  or  an ``e'' so that they will not look like  |
  299.      integer values.  For example,                                 |
  300.  
  301.           expr 20.0/5.0                                            |
  302.  
  303.      returns   ``4.0'',   not   ``4''.    The   global   variable  |
  304.      tcl_precision  determines  the  the  number  of  significant  |
  305.      digits that are retained when floating values are  converted  |
  306.      to  strings  (except  that trailing zeroes are omitted).  If  |
  307.      tcl_precision is unset then 6 digits of precision are  used.  |
  308.      To  retain  all of the significant bits of an IEEE floating-  |
  309.      point number set tcl_precision to 17;  if a  value  is  con-  |
  310.      verted  to  string with 17 digits of precision and then con-  |
  311.      verted back  to  binary  for  some  later  calculation,  the  |
  312.      resulting  binary value is guaranteed to be identical to the  |
  313.      original one.
  314.  
  315.  
  316. STRING OPERATIONS
  317.      String values may be used  as  operands  of  the  comparison
  318.      operators,  although  the  expression  evaluator tries to do
  319.      comparisons as integer or floating-point when  it  can.   If
  320.      one  of  the  operands  of  a comparison is a string and the
  321.      other has a numeric value, the numeric operand is  converted
  322.      back to a string using the C _s_p_r_i_n_t_f format specifier %d for
  323.      integers and %g for floating-point values.  For example, the
  324.      commands
  325.  
  326.  
  327.  
  328. Tcl                                                             5
  329.  
  330.  
  331.  
  332.  
  333.  
  334. expr(n)               Tcl Built-In Commands                   7.0
  335.  
  336.  
  337.  
  338.           expr {"0x03" > "2"}
  339.           expr {"0y" < "0x12"}
  340.  
  341.      both return 1.  The first comparison is done  using  integer
  342.      comparison,  and  the second is done using string comparison
  343.      after the second operand is converted to the string ``18''.
  344.  
  345.  
  346. KEYWORDS
  347.      arithmetic, boolean, compare, expression
  348.  
  349.  
  350.  
  351.  
  352.  
  353.  
  354.  
  355.  
  356.  
  357.  
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369.  
  370.  
  371.  
  372.  
  373.  
  374.  
  375.  
  376.  
  377.  
  378.  
  379.  
  380.  
  381.  
  382.  
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.  
  392.  
  393. Tcl                                                             6
  394.  
  395.  
  396.  
  397.